import java.awt.Color;

import info.gridworld.actor.Actor;

import info.gridworld.actor.Bug;
import info.gridworld.actor.Flower;
import info.gridworld.grid.Location;
import info.gridworld.grid.Grid;

public class Jumper extends Actor {

    public Jumper() {
        setColor(Color.RED);
    }

    public Jumper(Color col) {
        setColor(col);
    }

    public void act() {
        if (canMove()) {
            move();
        } else {
            turn();
        }
    }

    public void turn() {
        setDirection(getDirection() + Location.HALF_RIGHT);
    }

    public void move() {
        Grid<Actor> actor = getGrid();
        if (actor == null)
            return;
        Location location = getLocation();
        Location to = location.getAdjacentLocation(getDirection()).getAdjacentLocation(getDirection());
        if (actor.isValid(to)) {
            moveTo(to);
        } else {
            removeSelfFromGrid();
        }
    }

    public boolean canMove() {
        Grid<Actor> actor = getGrid();
        if (actor == null)
            return false;
        Location location = getLocation();
        Location to = location.getAdjacentLocation(getDirection()).getAdjacentLocation(getDirection());
        if (!actor.isValid(to)) {
            return false;
        }
        Actor neighbor = actor.get(to);
        return (neighbor == null) || (neighbor instanceof Flower);
    }
}
